import api from "../../apis/me";
import {formatDateToYYYYMMDD, formatTime} from "../../utils/dateUtil";

Page({
  data: {
    headerLayout: {
      title: "我的报名",
      paddingBottom: 7,
      back: true,
    },
    activityCardListLayout: [],
    containerPaddingBottom: 0,
    page: 1,
    size: 10,
  },
  addList(res, clear = false) {
    const daysMapping = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
    const tagMapping = ["科创", "生活", "就业", "社团", "讲座", "展览", "实验招募"]
    this.setData({
      page: res["current_page"],
      hasNext: res["has_next"],
      activityCardListLayout: [
        ...!clear ? this.data.cardListLayout[0] : [],
        ...res.data.map(e => {
          let date = new Date(e["activity_time"] * 1000);
          return {
            id: e.id,
            image: e["poster"] ?? `https://source.unsplash.com/90x120/?coding&id=${Math.floor(Math.random() * (1000 - 100 + 1) + 100)}`,
            title: e.name,
            tags: [tagMapping[e["type_id"]]],
            datetime: {
              date: formatDateToYYYYMMDD(date),
              day: daysMapping[date.getDay()],
              time: formatTime(date),
            },
            avatar: e["publisher_avatar"] ?? `https://source.unsplash.com/12x12/?coding&id=${Math.floor(Math.random() * (1000 - 100 + 1) + 100)}`,
            organizer: e["publisher_name"],
            variant: "activity",
            status: {
              name: "已报名",
              color: "#3DC232"
            }
          };
        })
      ]
    })
  },
  async loadMore() {
    if (!this.data.hasNext) return;
    const res = await api.getRegisteredActivities(this.data.page + 1, this.data.len);
    this.addList(res);
  },
  async onRefresh() {
    console.log("get");
    const res = await api.getRegisteredActivities(1, this.data.size);
    this.addList(res, true);
    this.selectComponent(".activity-card-list-wrapper").finishRefresh();
  },
  onLoad() {
    this.onRefresh();
    let info = wx.getSystemInfoSync()
    this.setData({
      containerPaddingBottom: info.windowHeight - info.safeArea.bottom
    })
  }
})
